package com.ujiuye.controller;

import com.ujiuye.domain.dto.PageDTO;
import com.ujiuye.domain.dto.SaveCategoryDTO;
import com.ujiuye.domain.po.Category;
import com.ujiuye.domain.vo.CategoryListVO;
import com.ujiuye.domain.vo.PageVO;
import com.ujiuye.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shiminghui
 * @date 2024/8/21 10:52
 * @description: TODO
 */
@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/list")
    public PageVO<CategoryListVO> list(PageDTO<String> pageDTO) {
        PageVO<CategoryListVO> pageVO = categoryService.page(pageDTO);
        return pageVO;
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/findAll")
    public List<CategoryListVO> findAll() {
        List<CategoryListVO> categoryListVOList = categoryService.findALl();
        return categoryListVOList;
    }

    /**
     * 根据分类id查询分类信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CategoryListVO getById(@PathVariable int id) {
        CategoryListVO categoryListVO = categoryService.getById(id);
        return categoryListVO;
    }

    /**
     * 保存分类信息
     *
     * @param saveCategoryDTO
     */
    @PostMapping("/save")
    public void saveCategory(@RequestBody SaveCategoryDTO saveCategoryDTO) {
        categoryService.saveCategory(saveCategoryDTO);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable int id) {
        log.info("删除的id{}", id);
        categoryService.deleteById(id);
    }

}
